package com.fc.mall.decoration.page.dao;

import com.fc.mall.decoration.page.model.CustomPage;
import com.fc.mall.decoration.page.object.PageStatus;
import com.fc.mall.mongo.util.MongoUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.fc.mall.util.Constants.NAME;
import static com.fc.mall.util.Constants.STATUS;
import static com.fc.mall.util.Constants.UNDERSCORE_ID;
import static com.fc.mall.util.Constants.UPDATED_AT;
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;

@Component
public class CustomPageRepositoryImpl implements ICustomPageRepository {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public CustomPage getCustomPage(String pageId, boolean activeOnly, Boolean isDefault) {
        Criteria criteria = where(UNDERSCORE_ID).is(pageId);
        if (activeOnly) {
            criteria.and(STATUS).is(PageStatus.ACTIVE);
        }
        if(isDefault != null) {
            criteria.and("isDefault").is(isDefault);
        }
        return mongoTemplate.findOne(query(criteria), CustomPage.class);
    }

    @Override
    public CustomPage getCustomPage(String pageId) {
        Criteria criteria = where(UNDERSCORE_ID).is(pageId);
        return mongoTemplate.findOne(query(criteria), CustomPage.class);
    }

    public List<CustomPage> getDefaultPages(){
        Criteria criteria = where("isDefault").is(true);
        return mongoTemplate.find(query(criteria), CustomPage.class);
    }

    public List<CustomPage> getAllActivePages(){
        Criteria criteria = new Criteria();
        criteria.and(STATUS).is(PageStatus.ACTIVE);
        return mongoTemplate.find(query(criteria), CustomPage.class);
    }

    @Override
    public Page<CustomPage> getCustomPages(boolean activeOnly, int page, int pageSize) {
        Criteria criteria = new Criteria();
        if (activeOnly) {
            criteria.and(STATUS).is(PageStatus.ACTIVE);
        }
        return MongoUtils.getPage(mongoTemplate, CustomPage.class, criteria, page, pageSize, Sort.by(Sort.Direction.DESC, UPDATED_AT));
    }

    @Override
    public Page<CustomPage> searchCustomPages(String keyword, boolean activeOnly, int page, int pageSize) {
        Criteria criteria = new Criteria();
        if (StringUtils.isNotBlank(keyword)) {
            Criteria keywordLikeCriteria = new Criteria().orOperator(
                    where(NAME).regex(keyword, "i"),
                    where("description").regex(keyword, "i"));
            criteria.andOperator(keywordLikeCriteria);
        }

        if (activeOnly) {
            criteria.and(STATUS).is(PageStatus.ACTIVE);
        }
        return MongoUtils.getPage(mongoTemplate, CustomPage.class, criteria, page, pageSize, Sort.by(Sort.Direction.DESC, UPDATED_AT));
    }
}
